package com.jushu.storbox;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.jushu.storbox.actions.BaseAction;
import com.jushu.storbox.constant.ClientConfig;
import com.jushu.storbox.constant.Constant;
import com.jushu.storbox.constant.MessageCode;
import com.jushu.storbox.util.ApplicationContext;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;
import com.huawei.sharedrive.sdk.android.util.StringUtil;

/**
 * 
 * LoginWaitActivity
 * 
 */
public class LoginWaitActivity extends BaseActivity {
	/** ACTIVITY TAG */
	private String TAG = "LoginWaitActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		LogUtil.i(TAG, "onCreate()");
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login_wait_bg_layout);
		// ActivityTaskManager.getInstance().putActivity(TAG,
		// LoginWaitActivity.this);
		ImageView bgimage = (ImageView) findViewById(R.id.wait_bg_iv);
		initData(bgimage);
	}

	/**
	 * LoginActivity init data
	 */
	private void initData(ImageView view) {

		if (PublicTools.isLocaleLanguageCN()) {
			view.setBackgroundResource(R.mipmap.about_logo);
		} else {
			view.setBackgroundResource(R.mipmap.about_logo_en);
		}
//		LogUtil.i(TAG, "step into LoginWaitActivity");
		SharedPreferences settings = getSharedPreferences(
				ClientConfig.settings, Context.MODE_APPEND);
		Editor editor = settings.edit();
		editor.putBoolean(ClientConfig.LOGINWAITACTIVITY, false);
		editor.commit();
		Handler handlers = new Handler();
		handlers.postDelayed(new Runnable() {
			public void run() {
				intoLoginActivity();
			}
		}, 1500);
	}

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@SuppressLint("ResourceAsColor")
		public void handleMessage(android.os.Message msg) {
			LogUtil.i(TAG, "Handle:" + msg.what);
			switch (msg.what) {
			case MessageCode.ASYN_UI_MESSAGE:
				if(Constant.getShareWithoutLogin(LoginWaitActivity.this)) {
					Intent intent = new Intent();
					intent.setClass(LoginWaitActivity.this, ShareActivity.class);
					startActivity(intent);
					LoginWaitActivity.this.finish();
				} else {
					Intent intent = new Intent();
					intent.setClass(LoginWaitActivity.this,
							MainFrameActivity.class);
					startActivity(intent);
					LoginWaitActivity.this.finish();
				}
				break;
			case MessageCode.LOGIN_EXCEPTION:
				Intent redirectLogin = new Intent();
				redirectLogin.setClass(LoginWaitActivity.this, LoginActivity.class);
				startActivity(redirectLogin);
				LoginWaitActivity.this.finish();
				break;
			default:
				break;

			}
		}
	};

	private void intoLoginActivity() {
		if (StringUtil.isBlank(ShareDriveApplication.getInstance().getAuthorization())
				|| Constant.getShareWithoutLogin(this)) {
			new Thread(new Runnable() {
                @Override
                public void run() {
                	try {
    					String oldAuthorization = ShareDriveApplication
    							.getInstance().getAuthorization();
    					ApplicationContext.getInstance().setRedirectLoginSuccess(true);
    					new BaseAction().loginInV2(
    						LoginWaitActivity.this,
    						PublicTools.getLoginUserName(LoginWaitActivity.this),
    						PublicTools.getLoginUserPassFail(LoginWaitActivity.this));
    					String newAuthorization = ShareDriveApplication
    							.getInstance().getAuthorization();
    					if (newAuthorization != null
    							&& !newAuthorization.equals(oldAuthorization)) {
    						ApplicationContext.getInstance()
    								.setRedirectLoginSuccess(true);
    					} else {
    						ApplicationContext.getInstance()
    								.setRedirectLoginSuccess(false);
    					}
    					Message message = new Message();
            			message.what = MessageCode.ASYN_UI_MESSAGE;
            			handler.sendMessage(message);
    				} catch (Throwable e) {
    					if(!ShareDriveApplication.getInstance().getWifiController().getNetworkState()){
    						Message message = new Message();
                			message.what = MessageCode.ASYN_UI_MESSAGE;
                			handler.sendMessage(message);
    					}else{
    						ApplicationContext.getInstance().setRedirectLoginSuccess(
        							false);
        					Message message = new Message();
                			message.what = MessageCode.LOGIN_EXCEPTION;
                			handler.sendMessage(message);
    					}
    				}
                }
            }).start();
		} else {
			Intent intent = new Intent();
			intent.setClass(LoginWaitActivity.this, MainFrameActivity.class);
			startActivity(intent);
			LoginWaitActivity.this.finish();
		}
	}

}
